package com.aierkeji.call.b2c.util.sms;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.util.CollectionUtils;

import com.aierkeji.call.b2c.util.AppConfig;
import com.aierkeji.call.b2c.util.DateTimeUtils;
import com.aierkeji.call.b2c.util.HttpUtils;
import com.aierkeji.call.b2c.util.StringUtils;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * @Title 类名
 * @Description
 * @Date 2018/11/8
 * @Author xiexiaoling
 * @Version
 */
public class SendSms {
	private final Logger log = LogManager.getLogger(getClass());
	static ObjectMapper mapper = new ObjectMapper();
	private String systemName;
	private String yzm; 
	private String time;

	public Map<String, Object> getSmsMap() {
		Map<String, Object> map = new HashMap<>();
		map.put("yzm", yzm);
		map.put("systemName", systemName);
		map.put("time", time);
		return map;
	}


	public SendSms(String systemName, String yzm,String time) {
		super();
		this.systemName = systemName;
		this.yzm = yzm;
		this.time =time;
	}


	public boolean send(String content, String phone) throws Exception {
		if("0".equals(AppConfig.getProperty("sms.flag","1"))) {
			return true;
		}
		String merchNo = AppConfig.getProperty("sms.merchNo");
		String templateId = AppConfig.getProperty("sms.templateId");
		String merchkey = AppConfig.getProperty("sms.merchkey");
		String url = AppConfig.getProperty("sms.url");
		String bussCode = AppConfig.getProperty("sms.bussCode");
		String version = AppConfig.getProperty("sms.version");

		SmsMassSendRequest smsMassSendRequest = new SmsMassSendRequest();
		smsMassSendRequest.setMerchNo(merchNo);
		smsMassSendRequest.setBussCode(bussCode);
		smsMassSendRequest.setVersion(version);
		smsMassSendRequest.setTemplateId(templateId);
		smsMassSendRequest.setReqNo(System.currentTimeMillis() + "");// 交易流水，每次接口唯一
		Set<String> phoneSet = new HashSet<>();
		phoneSet.add(phone);
		smsMassSendRequest.setPhone(phoneSet);
		List<String> contentList = new ArrayList<>();
		contentList.add(content);
		smsMassSendRequest.setContent(contentList);

		String respFinal = mapper.writeValueAsString(smsMassSendRequest);

		Map<String, Object> reqMap = mapper.readValue(respFinal, Map.class);
		String sign = SignUtil.getSign(reqMap, merchkey);

		smsMassSendRequest.setSign(sign);

		try {
			String postParams = mapper.writeValueAsString(smsMassSendRequest);
			String result = HttpUtils.postMethodWithJsonHeader(url, postParams, null);
			boolean flag = true;
			if (StringUtils.isEmpty(result)) {
				flag = false;
			}
			Map<String, Object> resultMap = null;
			if (flag) {
				resultMap = mapper.readValue(result, Map.class);
				if (CollectionUtils.isEmpty(resultMap)) {
					flag = false;
				}
			}
			if (!flag) {
				log.info("sendMsgResultEmpty:", postParams, result);
				return flag;
			}
			sign = SignUtil.getSign(resultMap, merchkey);
			if (resultMap.get("sign").equals(sign) && "0000".equals(resultMap.get("resultCode"))) {
				return true;
			} else {
				log.info("sendMsgResultError:", postParams, result);
				return false;
			}
		} catch (Exception e) {
			log.error("", e);
		}
		return false;
	}

	public static void main(String[] args) throws Exception {
		// 测试环境测试
		String url = "http://120.31.129.213:9200/api/sms/send_sms";
		String merchNo = "0000000000010870";
		String merchkey = "YcVRD8FvMwQPRAcslXkU4LZ07IvNjxSE";
		String templateId = "TL20190627001421";
		// 生产地址
//      String url  = "http://121.201.18.177:9200/api/sms/send";
//      String merchNo = "生产账号找商服提供";
//      String merchkey =  "生产账号找商服提供";
//      String templateId = "生产模板找商服提供";

		SmsMassSendRequest smsMassSendRequest = new SmsMassSendRequest();
		smsMassSendRequest.setMerchNo(merchNo);
		smsMassSendRequest.setBussCode("SMS002");
		smsMassSendRequest.setVersion("0010");
		smsMassSendRequest.setTemplateId(templateId);
		smsMassSendRequest.setReqNo(System.currentTimeMillis() + "");// 交易流水，每次接口唯一
		Set<String> phoneSet = new HashSet<>();
		phoneSet.add("15019949268");
		smsMassSendRequest.setPhone(phoneSet);
		List<String> contentList = new ArrayList<>();
		contentList.add("【智联操作】本次操作的验证码为:22242，30分钟内有效。如非您本人操作，请忽略该短信。");
		smsMassSendRequest.setContent(contentList);

		String respFinal = mapper.writeValueAsString(smsMassSendRequest);

		Map<String, Object> reqMap = mapper.readValue(respFinal, Map.class);
		String sign = SignUtil.getSign(reqMap, merchkey);

		smsMassSendRequest.setSign(sign);

		try {
			String postParams = mapper.writeValueAsString(smsMassSendRequest);
			System.out.println("请求参数：" + postParams);
			String result = HttpUtils.postMethodWithJsonHeader(url, postParams, null);
			System.out.println("返回结果：" + result);
			Map<String, Object> resultMap = mapper.readValue(result, Map.class);
			sign = SignUtil.getSign(resultMap, merchkey);
			if (resultMap.get("sign").equals(sign)) {
				System.out.println("签名正确");
			} else {
				System.out.println("签名失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


	public void setSystemName(String systemName) {
		this.systemName = systemName;
	}


	public void setYzm(String yzm) {
		this.yzm = yzm;
	}

	
	public void setTime(String time) {
		this.time = time;
	}

}
